import 'package:flutter/material.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/entity/match/expert_views_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';
import 'plan_item.dart';

class PlanListItem extends StatefulWidget {
  const PlanListItem(
      {super.key,
      required this.planList,
      this.tabId,
      this.isMatchView = false,
      this.eventType,
      this.eventParam,
      this.jumpParam});

  final List<PlanEntity> planList;
  final int? tabId;
  final bool isMatchView;
  final String? eventType;
  final dynamic eventParam;
  final Map<String, String>? jumpParam;

  @override
  State<PlanListItem> createState() => _PlanListItemState();
}

class _PlanListItemState extends State<PlanListItem>
    with AutomaticKeepAliveClientMixin {
  var expend = false;
  // late int num = Random().nextInt(widget.planList.length);

  @override
  void didUpdateWidget(covariant PlanListItem oldWidget) {
    setState(() {
      // expend = false;
      // num = Random().nextInt(widget.planList.length);
    });
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (widget.planList.length == 1) {
      return PlanItem(
        tabId: widget.tabId,
        entity: widget.planList[0],
        eventType: widget.eventType,
        isMatchView: widget.isMatchView,
        jumpParam: widget.jumpParam,
      );
    }
    List<Widget> list = List.generate(
        widget.planList.length,
        (index) => Container(
            decoration: BoxDecoration(
                border: index == widget.planList.length - 1 || !expend
                    ? null
                    : const Border(
                        bottom: BorderSide(color: Colours.greyEE, width: 0.5))),
            child: PlanItem(
              tabId: widget.tabId,
              entity: widget.planList[index],
              isMatchView: widget.isMatchView,
              eventType: widget.eventType,
              isExpertDetailView: index == 0 ? false : true,
              jumpParam: widget.jumpParam,
            )));
    list.add(Container(
        padding: const EdgeInsets.only(bottom: 8),
        alignment: Alignment.topCenter,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            QsText(expend ? "收起方案" : "更多方案",
                fontSize: 12, color: Colours.grey99),
            Container(width: 2),
            Image.asset(
                width: 16,
                Utils.getImgPath(expend ? "arrow_up.png" : "arrow_down.png"))
          ],
        )).tap(() => setState(() => expend = !expend)));
    return Container(
      color: Colours.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: expend
            ? list
            : [
                PlanItem(
                  inList: true,
                  tabId: widget.tabId,
                  entity: widget.planList.first,
                  eventType: widget.eventType,
                  isMatchView: widget.isMatchView,
                  jumpParam: widget.jumpParam,
                ),
                list.last
              ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
